package com.example.test.mytestapplication.Utils;

/**
 * @author cuige E64667
 * @date 2018/9/4 19:28
 * @company Fotile智能厨电研究院
 * @description XXXX
 */

public class StringUtils {

    /*
    * 判断是否是回文
    */
    public boolean isPalindrome(String str) {
        int len = str.length();
        boolean isPalindrome = false;

        String cstr = str.replaceAll("[\\p{Punct}\\p{Space}]+", "");  //去掉字符串中的标点与空格
        for (int i = 0; i <= len / 2; i++) {
            if (cstr.charAt(i) != cstr.charAt(len - i - 1)) {
                break;
            } else {
                isPalindrome = true;
            }
        }

        return isPalindrome;
    }

    /*
    * 判断是否包含子字符串
    * string:原始字符串
    * substr:子字符串
    */

    public boolean isContains(String string, String substr) {
        boolean isContains = false;
        int k = 0;

        for (int j = 0; j < string.length() - substr.length() + 1; j++) {
            if (string.charAt(j) == substr.charAt(0))  //如果首字母匹配，开始查找
            {
                for(int i = 0; i < substr.length() ; i++){
                    if(string.charAt(j+i) != substr.charAt(i)){  //string从j开始的字符与substr匹配，那ok
                        break;
                    }
                    k++;
                }
                if(k == substr.length()){
                    isContains = true;
                }
            }
        }
        return isContains;
    }
}
